package org.chinarjy.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.chinarjy.api.PersonApi;
import org.chinarjy.dto.PersonInfo;
import org.chinarjy.dto.ResultApi;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/person")
public class MyController {
    @DubboReference(version = "${dubbo.provider.version}",check = false,application = "${dubbo.application.id}")
    private PersonApi personApi;

    @RequestMapping("/get")
    public ResultApi<List<PersonInfo>> getAllPersons(){
        log.info("MyController -->> getAllPersons");
        return personApi.getAllPersons();
    }

    @RequestMapping("avgAge")
    public double avgAge(){
        double result = 0;
        int i = 0;
        List<PersonInfo> personInfos = personApi.getAllPersons().getData();
        if(personInfos.size()>0){
            for(PersonInfo personInfo :personInfos){
                result += personInfo.getAge();
            }
            result = (result/personInfos.size());
        }
        return result;
    }
}
